package com.lyj.linearalgebra.matrix.service;

import com.lyj.linearalgebra.matrix.dao.MatrixDao;
import com.lyj.linearalgebra.matrix.domain.Matrix;

public class MatrixService {

	private MatrixDao matrixDao = new MatrixDao();

	/**
	 * 矩阵的相加
	 */
	public Matrix matrixAdd(Matrix matrix_a, Matrix matrix_b) throws MatrixException {

		if(matrix_a.getRow() != matrix_b.getRow() || matrix_a.getCol() != matrix_b.getCol()) {
			throw new MatrixException("不能相加!");
		}
		return matrixDao.matrixAdd(matrix_a, matrix_b);
	}

	/**
	 * 矩阵的相乘
	 */
	public Matrix matrixMultiply(Matrix matrix_a, Matrix matrix_b) throws MatrixException {

		if (matrix_a.getCol() != matrix_b.getRow()) {
			throw new MatrixException("这两个矩阵不能相乘！");
		}
		return matrixDao.matrixMultiply(matrix_a, matrix_b);
	}

	/**
	 * 矩阵的转置
	 */
	public Matrix matrixTranspose(Matrix matrix) {
		return matrixDao.matrixTranspose(matrix);
	}

	/**
	 * 矩阵的秩
	 * 
	 */
	public int matrixRank(Matrix matrix) {
		return matrixDao.matrixRank(matrix);
	}

	/**
	 * 行列式的值
	 * 
	 * @throws MatrixException
	 */
	public double determinant(Matrix matrix) throws MatrixException {
		if (matrix.getRow() != matrix.getCol()) {
			throw new MatrixException("这不是行列式!");
		}
		return matrixDao.determinant(matrix);
	}

	/**
	 * 逆矩阵
	 * 
	 * @throws MatrixException
	 */

	public Matrix matrixInverse(Matrix matrix) throws MatrixException {
		if (matrix.getRow() != matrix.getCol()) {
			throw new MatrixException("该矩阵没有逆矩阵!");
		}
		return matrixDao.matrixInverse(matrix);
	}
	
	
	/**
	 * 齐次线性方程组的解
	 */

	public Matrix equationsSolution(Matrix matrix) {
		return matrixDao.equationsSolution(matrix);
	}
	
	/**
	 * 非齐次线性方程组的解
	 */
	
	public Matrix unequationsSolution(Matrix augmentMatrix) {
		return matrixDao.unequationsSolution(augmentMatrix);
	}
	
	
	/**
	 *向量组线性相关的判断
	 *
	 */
	
	public boolean isCorrelate(Matrix matrix) {
		return matrixDao.isCorrelate(matrix);
	}
	
}
